<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	// Make sure this file isn't being accessed directly
	if(!defined('IN_X7CHAT3')) return;
	
	$GLOBALS['OUTPUT']->cpmenu = array($GLOBALS['LANG']->lang_usermgmt->ucp_settings=>array($GLOBALS['LANG']->lang_usermgmt->ucp_preferences=>'general',$GLOBALS['LANG']->lang_usermgmt->ucp_ignorelist=>'ignorelist'));
	
	if($ses->user->has_permission('USE_BUDDY_LIST'))
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_usermgmt->ucp_settings][$GLOBALS['LANG']->lang_usermgmt->ucp_buddylist] = 'buddylist';
		
	if($ses->user->data_cache['group'] != lib_group::GUEST_GROUP_GID)
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_usermgmt->ucp_settings][$GLOBALS['LANG']->lang_usermgmt->ucp_profile] = 'profile';

	$section = lib_input::read('section','general');
	$GLOBALS['OUTPUT']->cpsection = $section;
	
	switch($section){
		case 'general':
			// Construct the form
			$form = new x7form();
			
			$form->add_section('general',$GLOBALS['LANG']->lang_usermgmt->ucp_preferences);
			$form->add_field_hidden('general','update_general','1');
			$form->add_field_checkbox('general','show_styles',$GLOBALS['LANG']->lang_usermgmt->show_styles,'',$ses->user->data_cache['settings']['show_styles']);
			$form->add_field_checkbox('general','play_sounds',$GLOBALS['LANG']->lang_usermgmt->play_sounds,'',$ses->user->data_cache['settings']['play_sounds']);
			$form->add_field_checkbox('general','show_smilies',$GLOBALS['LANG']->lang_usermgmt->show_smilies,'',$ses->user->data_cache['settings']['show_smilies']);
			$form->add_field_checkbox('general','show_timestamps',$GLOBALS['LANG']->lang_usermgmt->show_timestamps,'',$ses->user->data_cache['settings']['show_timestamps']);
			$form->add_field_checkbox('general','hide_email',$GLOBALS['LANG']->lang_usermgmt->hide_email,'',$ses->user->data_cache['settings']['hide_email']);
			
			$settings['style_selectors']['color'][$GLOBALS['LANG']->lang_usermgmt->default] = '';
			$settings['style_selectors']['size'][$GLOBALS['LANG']->lang_usermgmt->default] = '';
			$settings['style_selectors']['font'][$GLOBALS['LANG']->lang_usermgmt->default] = '';
			
			$form->add_section('styles',$GLOBALS['LANG']->lang_usermgmt->message_styles);
			$form->add_field_select('styles','color',$GLOBALS['LANG']->lang_usermgmt->color,$settings['style_selectors']['color'],$ses->user->data_cache['settings']['styles']['color']);
			$form->add_field_select('styles','font',$GLOBALS['LANG']->lang_usermgmt->font,$settings['style_selectors']['font'],$ses->user->data_cache['settings']['styles']['font']);
			$form->add_field_select('styles','size',$GLOBALS['LANG']->lang_usermgmt->size,$settings['style_selectors']['size'],$ses->user->data_cache['settings']['styles']['size']);
			
			$res = $form->get_results();
			if($res){
				$ses->user->data_cache['settings']['show_styles'] = $res['show_styles'];
				$ses->user->data_cache['settings']['play_sounds'] = $res['play_sounds'];
				$ses->user->data_cache['settings']['show_smilies'] = $res['show_smilies'];
				$ses->user->data_cache['settings']['show_timestamps'] = $res['show_timestamps'];
				$ses->user->data_cache['settings']['hide_email'] = $res['hide_email'];
				
				$ses->user->data_cache['settings']['styles']['font'] = $res['font'];
				$ses->user->data_cache['settings']['styles']['color'] = $res['color'];
				$ses->user->data_cache['settings']['styles']['size'] = $res['size'];
				
				$ses->user->commit_changes();
				$form->form_message($GLOBALS['LANG']->lang_usermgmt->preferences_updated);
			}
			
			echo tpl_usercp::settings_form($form);
			return;
		break;
		case 'profile':
			
			// Construct the form
			$form = new x7form();
			
			$form->add_section('email',$GLOBALS['LANG']->lang_usermgmt->email);
			$form->add_field_hidden('email','update_profile','1');
			$form->add_field_text('email','email',$GLOBALS['LANG']->lang_usermgmt->email,$ses->user->data_cache['email']);
			$form->add_field_checkbox('email','hide_email',$GLOBALS['LANG']->lang_usermgmt->hide_email,'',$ses->user->data_cache['settings']['hide_email']);
			
			$form->add_section('profile',$GLOBALS['LANG']->lang_usermgmt->ucp_profile);
			$form->add_field_text('profile','name',$GLOBALS['LANG']->lang_usermgmt->name,isset($ses->user->data_cache['profile']['name']) ? $ses->user->data_cache['profile']['name'] : '');
			$form->add_field_text('profile','location',$GLOBALS['LANG']->lang_usermgmt->location,isset($ses->user->data_cache['profile']['location']) ? $ses->user->data_cache['profile']['location'] : '');
			$form->add_field_text('profile','hobbies',$GLOBALS['LANG']->lang_usermgmt->hobbies,isset($ses->user->data_cache['profile']['hobbies']) ? $ses->user->data_cache['profile']['hobbies'] : '');
			$form->add_field_select('profile','gender',$GLOBALS['LANG']->lang_usermgmt->gender,array($GLOBALS['LANG']->lang_usermgmt->male=>1,$GLOBALS['LANG']->lang_usermgmt->female=>2),isset($ses->user->data_cache['profile']['gender']) ? $ses->user->data_cache['profile']['gender'] : '');
			
			$form->add_section('password',$GLOBALS['LANG']->lang_usermgmt->password);
			$form->add_field_pass('password','oldpass',$GLOBALS['LANG']->lang_usermgmt->current_password);
			$form->add_field_pass('password','pass1',$GLOBALS['LANG']->lang_usermgmt->new_password,$GLOBALS['LANG']->lang_usermgmt->only_if_changing);
			$form->add_field_pass('password','pass2',$GLOBALS['LANG']->lang_usermgmt->confirm_password,$GLOBALS['LANG']->lang_usermgmt->only_if_changing);
			
			$res = $form->get_results();
			if($res){
				
				if($res['email'] != $ses->user->data_cache['email']){
					
					if(!preg_match('/^[^@]*@[^.]*\..*$/',$res['email'])){
						// Invalid E-Mail
						$form->add_field_error('email',$GLOBALS['LANG']->lang_usermgmt->email_invalid_email,'');
					}else{
						
						$act = new lib_user($res['email'],'email');
						if($act->is_valid()){
							// E-Mail in use
							$form->add_field_error('email',$GLOBALS['LANG']->lang_usermgmt->email_taken_email,'');
						}
						
					}
					
				}
				
				$ses->user->data_cache['email'] = $res['email'];
				
				$ses->user->data_cache['settings']['hide_email'] = $res['hide_email'];
				
				if($ses->user->data_cache['password'] != sha1($res['oldpass'])){
					$form->add_field_error('oldpass',$GLOBALS['LANG']->lang_usermgmt->cur_password_error,'');
				}
				
				if(!empty($res['pass1'])){
					
					if($res['pass1'] != $res['pass2']){
						$form->add_field_error('pass1',$GLOBALS['LANG']->lang_usermgmt->new_password_error,'');
						$form->add_field_error('pass2',$GLOBALS['LANG']->lang_usermgmt->new_password_error,'');
					}
					
					$perror = lib_user_extended::validate_password($res['pass1']);
					if(!empty($perror)){
						$form->add_field_error('pass1',$perror,'');
						$form->add_field_error('pass2',$perror,'');
					}
					
					$ses->user->data_cache['password'] = sha1($res['pass1']);
				}
				
				$ses->user->data_cache['profile']['name'] = $res['name'];
				$ses->user->data_cache['profile']['location'] = $res['location'];
				$ses->user->data_cache['profile']['hobbies'] = $res['hobbies'];
				$ses->user->data_cache['profile']['gender'] = $res['gender'];
				
				if(!$form->has_errors()){
					$ses->user->commit_changes();
					$form->form_message($GLOBALS['LANG']->lang_usermgmt->profile_updated);
				}
			}
			
			echo tpl_usercp::profile_form($form);
			return;
		break;
		case 'buddylist':
			if(!$ses->user->has_permission('USE_BUDDY_LIST')){
				echo tpl_usercp::buddylist_disabled();
				return;
			}
			
			$form = new x7form();
			$form->add_section(1,$GLOBALS['LANG']->lang_usermgmt->add_a_buddy);
			$form->add_field_text(1,'add',$GLOBALS['LANG']->lang_usermgmt->username);
			
			$res = $form->get_results();
			if($res){
				if($ses->user->add_buddy($res['add']))
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_usermgmt->buddylist_added;
				else
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_usermgmt->buddylist_notadded;
			}
			
			$remove = lib_input::read('remove',null,lib_input::NUMBER);
			if(!empty($remove)){
				if($ses->user->remove_buddy($remove))
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_usermgmt->buddylist_removed;
				else
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_usermgmt->buddylist_notremoved;
			}

			echo tpl_usercp::buddylist($form,$ses->user->data_cache['buddies']);
			return;
		break;
		case 'ignorelist':
			
			$form = new x7form();
			$form->add_section(1,$GLOBALS['LANG']->lang_usermgmt->ignore_a_user);
			$form->add_field_text(1,'add',$GLOBALS['LANG']->lang_usermgmt->username);
			
			$res = $form->get_results();
			if($res){
				if($ses->user->ignore_user($res['add']))
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_usermgmt->ignore_added;
				else
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_usermgmt->ignore_notadded;
			}

			$remove = lib_input::read('remove',null,lib_input::NUMBER);
			if(!empty($remove)){
				if($ses->user->unignore_user($remove))
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_usermgmt->ignore_removed;
				else
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_usermgmt->ignore_notremoved;
			}

			echo tpl_usercp::ignorelist($form,$ses->user->data_cache['ignore']);
			return;
		break;
	}

?>